###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/ImportExport.pm.pl -> ImportExport module               #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::ImportExport;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = ("input" => {}, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "From here you can import and export tables in the SuperLinks database:", colspan => 2 }]);

  $Body .= $Dialog->FormRadio(
    name      => "Type",
    value     => "Export",
    subject   => "Action",
    required  => 1,
    items     => [
      { value => "Export", label => "Export", extra => { onClick => "form.action.value='ImportExport';form.submit();" } },
      { value => "Import", label => "Import", extra => { onClick => "form.action.value='ImportExport';form.submit();" } }
    ], join => "&nbsp;"
  );
  
  my @options;
  foreach my $table (@{ $in{'input'}->{'TABLES'} }) {
    push(@options, { value => $table, label => $table });
  }
  
  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_TABLE",
    subject   => "Table",
    required  => 1,
    items     => \@options
  );
  
  $Body .= $Dialog->FormRadio(
    name      => "FORM_FORMAT",
    value     => "Excel",
    subject   => "Format",
    required  => 1,
    items     => [
      { value => "Excel", label => "Excel workbook (*.xls)" },
      { value => "CSV", label => "Comma-seperated variable (CSV) file (*.csv)" }
    ], join => "<br>"
  );

  if ($SL::QUERY{'Type'} eq "Import") {
    $Body .= $Dialog->FormFileBox(
      name      => "FORM_DATA",
      subject   => "Data File",
      required  => 1
    );
  }

  $Body .= $Dialog->FormButton(
    items   => [
      { type => "submit", value => "Next >" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body      => $Body,
    header    => "Import/Export",
    form      => 1,
    multipart => 1,
    hiddens   => [
      { name => "action", value => "DoImportExport" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my ($section) = @_;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body  = $Dialog->TableRow(items => [{ body => "Importing table:", colspan => 2 }]);
     $Body .= $Dialog->TableRow(items => [{ body => "<iframe src=\"".$SL::CGI->self_url()."&do=1\" name=\"LOG\" height=\"300\" width=\"100%\"></iframe>", colspan => 2 }]);

  $Body .= $Dialog->FormButton(
    items => [
      { type => "button", value => "Close", extra => { onClick => "window.location='".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Backups&SID=$SL::ADMIN{'SID'}")."';" } }
    ]
  );
  
  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body        => $Body,
    header      => "Import/Export",
    body_extra  => "onLoad=\"document.LOG.scrollTo(0, 9999999);\""
  );
  
  return $Body;
}

1;